package com.navinfo.platform.trip.common.arithmetic;

import com.navinfo.platform.trip.common.arithmetic.data.CommonData;
import com.navinfo.platform.trip.common.arithmetic.data.OuterStatisticData;
import com.navinfo.platform.trip.common.arithmetic.var.VarCoastFlameout;
import com.navinfo.platform.trip.common.arithmetic.enums.EventStatus;
import com.navinfo.platform.trip.common.arithmetic.enums.EventType;
import com.navinfo.platform.trip.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.platform.trip.common.config.ConfigMap;
import com.navinfo.platform.trip.common.util.ArithmeticUtils;

import java.util.Optional;

/**
 * 东风熄火滑行算法<br/>
 * 依赖{@link Mileage}里程油耗算法<br/>
 * 输出：统计、事件<br/>
 * @author web
 */
public class CoastFlameout_DF extends AbstractArithmetic<VarCoastFlameout> {
    /**
     * 东风熄火滑行事件
     */
    private final EventType EVENT_TYPE = EventType.coastFlameout_DF;
    /**
     * 速度阀值 单位：km/h
     */
    private int speedThreshold = ConfigMap.getInteger("DF_FlameoutCoast_Speed_Threshold");
    /**
     * 转速阀值 单位：RPM
     */
    private int rotationThreshold = ConfigMap.getInteger("DF_FlameoutCoast_Rotation_Threshold");

    /**
     * 判断当前点是否为东风熄火滑行状态
     */
    boolean isCurFlameoutCoast() {
        CommonData data = getCommonData();
        return data.getRotation()<=rotationThreshold && data.getSpeedKmh()>speedThreshold;
    }

    @Override
    public void calculate() {
        logger.debug("进入东风熄火滑行算法.[terminalId:{}]", terminalId);
        VarCoastFlameout var = getObj();
        boolean preStatus = var.isPreFlameOutCoast();
        boolean curStatus = isCurFlameoutCoast();
        logger.debug("东风熄火滑行算法，东风熄火滑行状态：preStatus:{},curStatus:{}.[terminalId:{}]", preStatus, curStatus, terminalId);

        //算法计算
        EventStatus eventStatus = super.getEventStatus(preStatus, curStatus);
        switch (eventStatus) {
            case START:
                logger.debug("东风熄火滑行算法，事件开始.[terminalId:{}]", terminalId);
                startEventData(EVENT_TYPE);
                break;
            case IN:
                logger.debug("东风熄火滑行算法，事件中.[terminalId:{}]", terminalId);
                calcEventData(EVENT_TYPE);
                break;
            case END:
                logger.debug("东风熄火滑行算法，事件结束.[terminalId:{}]", terminalId);
                calcEventData(EVENT_TYPE);
                endEventData(var);
                break;
            default:
                logger.debug("东风熄火滑行算法，东风熄火滑行状态：上一次和本次都未东风熄火滑行，不进行计算.[terminalId:{}]", terminalId);
        }

        //行程结束时要清理对应的未结束事件
        if (getCommonData().isTripLastNode()) {
            endEventData(var);
        }

        var.setPreFlameOutCoast(curStatus);
        logger.debug("东风熄火滑行算法结束.[terminalId:{}]", terminalId);
    }

    private void endEventData(VarCoastFlameout var) {
        endEventData(EVENT_TYPE, eventData -> {
            var.addMileage( Optional.ofNullable(eventData.getExtendField05()).orElse(0.0));
            var.addFuel( Optional.ofNullable(eventData.getExtendField06()).orElse(0.0));
            var.addDuration( Optional.ofNullable(eventData.getDuration()).orElse(0L).intValue());
            var.addNumber(1);
        });
    }

    @Override
    public void collect(OuterStatisticData statisticDataOut) {
        VarCoastFlameout var = getObj();
        statisticDataOut.setFlameoutCoastMileage_DF(ArithmeticUtils.mileageMToM(var.getMileage()));
        statisticDataOut.setFlameoutCoastFuel_DF(ArithmeticUtils.fuelMlToMl(var.getFuel()));
        statisticDataOut.setFlameoutCoastDuration_DF(ArithmeticUtils.timeMsToSecond(var.getDuration()));
        statisticDataOut.setFlameoutCoastNumber_DF(var.getNumber());
    }
}
